Conditions | 1 |
Paths | 1 |
Total Lines | 81 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
20 | var ReceiveCallback = (function () { |
||
21 | 'use strict'; |
||
22 | var me = {}; |
||
23 | var $inputElem; |
||
24 | var $hiddenElem; |
||
25 | |||
26 | /** |
||
27 | * getOrgData - Returns the input data of the field the user can see. |
||
28 | * |
||
29 | * @private |
||
30 | * @return {string} |
||
31 | */ |
||
32 | function getOrgData() { |
||
33 | return $inputElem.text(); |
||
34 | } |
||
35 | |||
36 | /** |
||
37 | * setOrgData - Sets the value of the input field the user can see. |
||
38 | * |
||
39 | * @private |
||
40 | * @param {string} data The data to set. |
||
41 | * @return {string} |
||
42 | */ |
||
43 | function setOrgData(data) { |
||
44 | if ($inputElem.text() !== data) { |
||
|
|||
45 | return $inputElem.text(data); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * setHiddenData - Change data of the hidden input. |
||
51 | * |
||
52 | * @private |
||
53 | * @param {string} data The data to set. |
||
54 | * @return {string} |
||
55 | */ |
||
56 | function setHiddenData(data) { |
||
57 | return $hiddenElem.val(data); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * filterData - Filter the input data. |
||
62 | * |
||
63 | * @private |
||
64 | * @param {string} data The data to filter. |
||
65 | * @return {string} |
||
66 | */ |
||
67 | function filterData(data) { |
||
68 | // remove all bad characters |
||
69 | // https://regex101.com/r/g4Dkb7/1 |
||
70 | return data.replace(/[^\w_-]+/ig, ''); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * init - Initialize handler. |
||
75 | * |
||
76 | */ |
||
77 | me.init = function init() { |
||
78 | // set variables |
||
79 | $inputElem = $('.threemagw_receivecallback_input'); |
||
80 | $hiddenElem = $('.threemagw_receivecallback_hiddeninput'); |
||
81 | |||
82 | // register input trigger/event |
||
83 | $inputElem.on('input', me.update); |
||
84 | }; |
||
85 | |||
86 | /** |
||
87 | * update - Update the output field (and, if necessary, also the output |
||
88 | * field) |
||
89 | * |
||
90 | */ |
||
91 | me.update = function update() { |
||
92 | var data; |
||
93 | |||
94 | data = filterData(getOrgData()); |
||
95 | setOrgData(data); |
||
96 | setHiddenData(data); |
||
97 | }; |
||
98 | |||
99 | return me; |
||
100 | })(); |
||
101 |
This check looks for functions where a
return
statement is found in some execution paths, but not in all.Consider this little piece of code
The function
isBig
will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly returnundefined
.This behaviour may not be what you had intended. In any case, you can add a
return undefined
to the other execution path to make the return value explicit.